Imports System.Text
Imports System.IO
Imports OpenNETCF.Compression

Module Module1

    Sub Main()
        TestGZWriteStream()
        TestGZReadStream()
        TestCompressDecompress()

        TestInflateDeflate()
    End Sub

    Sub TestCompressDecompress()
        Dim source As String = "A quick brown fox jumped over lazy dogs"

        Dim src() As Byte = Encoding.ASCII.GetBytes(source)
        Dim dst(1024) As Byte

        Dim compressedSize As Integer
        Dim ret As ErrorCode = zlibCE.Compress(dst, src, compressedSize)

        Dim uncompressedSize As Integer
        ReDim src(1024)
        ret = zlibCE.Uncompress(src, dst, uncompressedSize)
    End Sub

    ' Test deflate/inflate stream-to-stream (no headers, not a valid gzip file)
    Sub TestInflateDeflate()
        Dim source As String = "A quick brown fox jumped over lazy dogs"

        Dim wrt As New StreamWriter("\Storage Card\in.txt", False)
        For i As Integer = 0 To 100
            wrt.WriteLine(source)
        Next
        wrt.Close()

        Dim inf As Stream = File.OpenRead("\Storage Card\in.txt")
        Dim outf As Stream = File.Create("\Storage Card\out.gz")

        zlibCE.Deflate(inf, outf, 9)

        inf.Close()
        outf.Close()

        inf = File.OpenRead("\Storage Card\out.gz")
        outf = File.Create("\Storage Card\out.txt")

        zlibCE.Inflate(inf, outf)
    End Sub


    ' Test deflating to stream
    Sub TestGZWriteStream()
        Dim stm As GZStream = GZStream.OpenWrite(File.Create("\Storage Card\test.gz"), CompressionLevel.Level5)
        Dim source As String = "A quick brown fox jumped over lazy dogs\r\n"

        For i As Integer = 0 To 100
            Dim line() As Byte = Encoding.ASCII.GetBytes(source)
            stm.Write(line, 0, line.Length)
        Next

        stm.Close()
    End Sub

    ' Test inflating from gzipped file/stream (with the header)
    Sub TestGZReadStream()
        Dim stm As GZStream = GZStream.OpenRead(File.OpenRead("\Storage Card\test.gz"))
        Dim stmOut As Stream = File.Create("\Storage card\test.txt")

        Dim buf(8192) As Byte
        Dim cb As Integer = buf.Length

        Do While ((cb = stm.Read(buf, 0, cb) <> 0))
            stmOut.Write(buf, 0, cb)
            cb = buf.Length
        Loop

        stm.Close()
        stmOut.Close()
    End Sub

End Module
